﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using System.Web.Http.Controllers;
using Sharp.Infrastructure;

namespace Sharp.Portal.WebAPI.Filter
{
    /// <summary>
    /// 标头增加特定参数
    /// </summary>
    [DebuggerStepThrough]
    public class HttpHeaderActionFilter : System.Web.Http.Filters.IActionFilter
    {
        /// <summary>
        /// 多重
        /// </summary>
        public bool AllowMultiple { get; }
        /// <summary>
        /// ExecuteActionFilterAsync
        /// </summary>
        /// <param name="actionContext"></param>
        /// <param name="cancellationToken"></param>
        /// <param name="continuation"></param>
        /// <returns></returns>
        public Task<HttpResponseMessage> ExecuteActionFilterAsync(HttpActionContext actionContext, CancellationToken cancellationToken, Func<Task<HttpResponseMessage>> continuation)
        {
            IEnumerable<string> values;
            if (actionContext.Request.Headers.TryGetValues("currentUser", out values))
            {
                ContactContext.Current.UserId = int.Parse(values.ToArray()[0]);
            }


            HttpResponseMessage response = continuation().Result;

            return Task.FromResult(response);
        }
    }
}